package com.sharemarking.application.wechat.controller;

import com.sharemarking.application.common.utils.MessageUtil;
import com.sharemarking.application.common.utils.WechatSignUtil;
import com.sharemarking.application.wechat.dto.TextMessageDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/4.
 */
@Controller
@RequestMapping("msg")
public class MsgController {

    @Autowired
    WechatSignUtil wechatSignUtil;

    @RequestMapping(value = "index", method = RequestMethod.GET)
    public void get(HttpServletRequest request,  HttpServletResponse response) throws ServletException, IOException {
        // 微信加密签名
        String signature = request.getParameter("signature");
        // 时间戳
        String timestamp = request.getParameter("timestamp");
        // 随机数
        String nonce = request.getParameter("nonce");
        // 随机字符串
        String echostr = request.getParameter("echostr");

        PrintWriter out = response.getWriter();
        // 通过检验signature对请求进行校验，若校验成功则原样返回echostr，表示接入成功，否则接入失败
        if (wechatSignUtil.checkSignature(signature, timestamp, nonce)) {
            out.print(echostr);
        }
        out.close();
    }

    @RequestMapping(value = "index", method = RequestMethod.POST)
    public void post(HttpServletRequest request, HttpServletResponse response){
        PrintWriter out = null;
        try {
            request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8");
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Map<String, String> map = MessageUtil.xmlToMap(request);
        String FromUserName = map.get("FromUserName");
        String ToUserName = map.get("ToUserName");
        String MsgType = map.get("MsgType");
        String Content = map.get("Content");

        System.out.println("content===="+Content);

        String message = null;
        if ("text".equals(MsgType)) {
            TextMessageDto text = new TextMessageDto();
            text.setFromUserName(ToUserName);
            text.setToUserName(FromUserName);
            text.setMsgType("text");
            text.setCreateTime(System.currentTimeMillis());
            if (Content.equals("1")){
                text.setContent("您选择的是1号虾蟹煲");
            } else if (Content.contains("2")){
                text.setContent("您选择的是"+Content+"号糖醋排骨");
            } else {
                text.setContent("奇迹要出现啦！！");
            }
            message = MessageUtil.textMessageToXml(text);
        }
        out.println(message);
        out.close();
    }
}
